﻿using AutoMapper;
using B.S.Filtration.Domain.到货检验;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.到货检验.PurchaseInfoCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.到货检验.PurchaseInfoHandler
{
    public class AddPurchaseInfoHandler : IRequestHandler<AddPurchaseInfoCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;
        public AddPurchaseInfoHandler(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }
        public async Task<APIResult<object>> Handle(AddPurchaseInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<PurchaseInfo>(request);
            //string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            //string ProductCodess1 = "CG" + yeartime;
            //model.PurchaseOrderNumber = ProductCodess1;
            model.IsDel = false;

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "添加失败";
            }
            return result;
        }
    }
}
